from flask import request, current_app, g
from ..utils.response import Response
from ..utils.security import token_manager


def jwt_authenticate():
    white_list = current_app.config.get("WHITE_LIST")
    if request.path.startswith("/uploads"):
        return
    elif request.path in white_list:
        return
    else:
        token = request.headers.get("Authorization")
        if not token:
            return Response.error(msg="用户认证失败")
        code, payload = token_manager(current_app).verify_jwt(token)
        if isinstance(payload, str):
            return Response.error(msg=payload)
        g.user = payload
